﻿using System;
using System.Data;
using System.Web;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;


public partial class download : System.Web.UI.Page
{
    //建立数据库连接
    public static SqlConnection conn()
    {
        string sqlstr = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(sqlstr);
        return con;
    }
    SqlConnection Con = conn();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)//首次加载
        {
            addListBox();
        }
        //showfile();

    }
    //public void showfile()
    //{
    //    using (Con)
    //    {
    //        int ImgID = 2;
    //       // int ImgID = Convert.ToInt32(Request.QueryString["imageId"]); //ID为图片ID 
    //        //建立数据库链接
    //        //string strCon = System.Configuration.ConfigurationManager.AppSettings["DSN"];
    //        //SqlConnection Con = new SqlConnection(strCon);
    //        String SqlCmd = "SELECT * FROM ImageFile WHERE imageId = @ImageID";
    //        SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
    //        CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID;
    //        Con.Open();
    //        SqlDataReader SqlReader = CmdObj.ExecuteReader();
    //        SqlReader.Read();
    //        Response.ContentType = (string)SqlReader["imageContentType"];//设定输出文件类型
    //        //输出图象文件二进制数制
    //      //  Response.OutputStream.Write((Byte[])SqlReader["imagepath"], 0, (int)SqlReader["imageSize"]);
    //        Response.End();
       

    //        Con.Close();
    //    }
    //}
    protected void addListBox()
    {
        //将指定文件夹中的文件保存到字符串数组中
        string[] name = Directory.GetFiles(Server.MapPath("UpFile/UpLoad/File/"));
        foreach (string s in name)
        {
            //将文件名添加到ListBox中
            ListBox1.Items.Add(Path.GetFileName(s));
        }
    }

    protected void dFile()
    {
        //判断是否选择文件名
        if (ListBox1.SelectedValue != "")
        {
            if (Session["txt"] != "")
            {   //获取文件路径
                string path = Server.MapPath("UpFile/UpLoad/File/") + Session["txt"].ToString();
                //初始化 FileInfo 类的实例，它作为文件路径的包装
                FileInfo fi = new FileInfo(path);

                //判断文件是否存在
                if (fi.Exists)
                {
                    //将文件保存到本机上
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
                    Response.AddHeader("Content-Length", fi.Length.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.Filter.Close();
                    Response.WriteFile(fi.FullName);
                    Response.End();
                }
            }
        }
        else
        {
            Page.RegisterStartupScript("sb3", "<script>alert('请先选择需要下载的文件，谢谢！')</script>");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        dFile();
    }
    protected void ListBox1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        Session["txt"] = ListBox1.SelectedValue.ToString();
    }
}
